package com.maycurobj.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

//常用工具类
@Slf4j
public class NormalUtil {


    /**
     * 获取guid
     * @return
     */
    public static String getGuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }
    /**
     * 转义html 转码
     *
     * @param encodeString
     * @return
     */
    public static String htmlEncode(String encodeString) {
        encodeString = encodeString.replace("<", "&lt;");
        encodeString = encodeString.replace(">", "&gt;");
        encodeString = encodeString.replace("'", "&#39;");
        encodeString = encodeString.replace("\"", "&quot;");
        return encodeString;
    }

    /**
     * 转义html 解码
     *
     * @param decodeString
     * @return
     */
    public static String htmlDecode(String decodeString) {
        decodeString = decodeString.replace("&lt;", "<");
        decodeString = decodeString.replace("&gt;", ">");
        decodeString = decodeString.replace("&#39;", "'");
        decodeString = decodeString.replace("&quot", "\"");
        return decodeString;
    }

    public static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }

    public static String getUpperGuid() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        return uuid.toString().toUpperCase();

    }

    public static String getLowerGuid() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        return uuid.toString().toLowerCase();

    }

    /**
     * 获取当前时间戳
     * @return
     */
    public static long nowDateToLong() {
        return new Date().getTime();
    }

    /**
     * 长时间转为短时间
     * @param s
     * @return
     */
    public static String longTimeToShortTime(String s) {
        try {
            SimpleDateFormat longTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat shortTime=new SimpleDateFormat("yyyy-MM-dd");
            Date date = longTime.parse(s);
            return shortTime.format(date);
        } catch (Exception e) {
            log.info("时间转换异常-----longTimeToShortTime---{}", e);
            return null;
        }
    }


    /**
     * 日期戳转为yyyy-MM-dd
     *
     * @param l
     * @return
     */
    public static String longToShortTime(long l) {
        try {
            SimpleDateFormat shortTime = new SimpleDateFormat("yyyy-MM-dd");
            // 使用 Date 对象进行格式化
            Date date = new Date(l);
            return shortTime.format(date);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 字符串时间 转时间戳
     * @param _s
     * @return
     */
    public static long dateToLong(String _s) {
        try{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(_s);
            return date.getTime();
        }catch (Exception ex){
            ex.printStackTrace();
            return new Date().getTime();
        }
    }

    /**
     * 日期时间 转 long型
     * @param _s
     * @return
     */
    public static long dateTimeToLong(String _s) {
        try{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date=sdf.parse(_s);
            return date.getTime();
        }catch (Exception ex){
            ex.printStackTrace();
            return new Date().getTime();
        }
    }

    public static long dateTimeMsToLong(String _s) {
        try{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            Date date=sdf.parse(_s);
            return date.getTime();
        }catch (Exception ex){
            ex.printStackTrace();
            return new Date().getTime();
        }
    }

    /**
     * 带SSS的当前时间
     *
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return dateFormat.format(new Date());
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getNowData() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date());
    }

    /**
     * 获取格林威志当前时间
     *
     * @return
     */
    public static String getGMTDateTime() {
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, -8);
        return sdf.format(calendar.getTime());
    }

    /**
     * 通过逗号分割的字符串转list
     *
     * @return
     */
    public static List<Object> strToList(String str) {
        log.info("需要同步的平台:" + str);
        try {
            if (str.length() > 0) {
                String[] args = str.split(",");
                List<Object> list = new ArrayList<>();
                for (Object o : args) {
                    list.add(o);
                }
                return list;
            } else {
                return new ArrayList<>();
            }
        } catch (Exception ex) {
            return new ArrayList<>();
        }
    }

    /**
     * 转义crmurl
     *
     * @param _s
     * @return
     */
    public static String encodeUrl(String _s) {
        try {
            String __s = "";
            __s = _s.replace("'", "%27");
            __s = _s.replace(" ", "%20");

            return __s;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    /**
     * 分页方法
     *
     * @param _pageSize  每页记录数
     * @param _pageIndex 页码 必须大于0
     * @param inList     待分页List
     * @param <T>
     * @return
     */
    public static <T> List<T> paging(int _pageIndex, int _pageSize, List<T> inList) {

        int pageSize = _pageSize;
        int count = inList.size();
        int pageNum = _pageIndex;

        List<T> outList = new ArrayList<>();
        int startIndex = pageSize * pageNum + 1;
        int endIndex = startIndex + pageSize;
        for (int i = startIndex; i < endIndex; i++) {
            if (i <= count) {
                outList.add(inList.get(i - 1));
            }
        }
        log.info("分页执行结束");
        return outList;
    }

    /**
     * 获取map value 否则返回null
     *
     * @param map
     * @param key
     * @return
     */
    public static <T> T getMapValue(Map<String, T> map, String key) {
        try {
            Map map1 = map;
            if (map1.containsKey(key)) {

                return (T) map1.get(key);
            } else {
                return null;
            }
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 字符串转数字
     * @param _s
     * @return
     */
    public static Integer stringToInt(String _s){
        try{
            if("".equals(_s)){
                return null;
            }else{
                return Integer.valueOf(_s);
            }
        }catch(Exception ex){
            return null;
        }
    }

    /**
     * null 转 ""
     * @param _s
     * @return
     */
    public  static String null2String(Object _s){
        try{
            return _s==null?"":String.valueOf(_s);
        }catch (Exception ex){
            return "";
        }
    }

    public static long null2Long(Object object) {
        try {
            if (Objects.isNull(object)) {
                return 0;
            }else{
                return Long.valueOf(object.toString());

            }
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static double null2Double(Object object) {
        try {
            if (Objects.isNull(object)) {
                return 0.00;
            }else{
                return Double.valueOf(object.toString());

            }
        } catch (Exception e) {
            return 0.00;
        }
    }


}
